<?php

namespace App\Console\Commands;

use App\Models\Common\Common;
use App\Models\Tools\Data;
use App\Models\Tools\Message;
use App\Models\Tools\Period;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExploreRemind extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:explore_remind';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '探店未完成提醒';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /**
         * 待提交状态的超时工单
         */
        try{
            $we_ids = DB::table('explore_work as ew')
                ->leftJoin('explore_order as eo','ew.eo_id','=','eo.eo_id')
                ->where('ew.status', '=', Data::DATA_DB_EXPLORE_WORK_STATUS_INIT)
                ->where('ew.receive_time', '<', date('Y-m-d H:i:s', time() - Period::getSetting('EXPLORE_UPLOAD_LIMIT_TIME')))
                ->pluck('ew.receiver_we_id');
            $we_ids = DB::table('explore_work')
                ->where('status', '=', Data::DATA_DB_EXPLORE_WORK_STATUS_REFUSE)
                ->where('hang_up','<>',1)//挂起的工单暂不处理
                ->pluck('receiver_we_id')
                ->union($we_ids)
                ->unique()
                ->toArray();
            $we_ids = json_decode(json_encode($we_ids), true);
            // 查询需要通知人的openid
            $receiver_arr = DB::table('lottery_users')->whereIn('we_id',$we_ids)->select('openid','phone')->get()->toArray();
            $receiver_arr = json_decode(json_encode($receiver_arr),true);
            //  发送验收拒回微信通知（发送给达人）
            $data = [
                'first' => '您有探店任务尚未提交',
                'keyword1' => '-',
                'keyword2' => '-',
                'remark' => '请在接单后48小时内提交投稿，如果超时，系统将会自动撤回订单。',
            ];
            //发送通知
            foreach($receiver_arr as $receiver){
                Common::sendLaifeiMessage($receiver['openid'],'work_timeout',$data,'/talent/history?tab=1');
                Message::exploreRemind($receiver['phone']);
            }
            if(count($receiver_arr)){
                Log::debug('工单超时提醒定时任务，推送达人：',$receiver_arr);
            }
        }catch (\Exception $e){
            Log::emergency(ExploreRemind::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }
        return true;
    }


}
